package com.probiz.estore.content.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.catalog.dao.ContentCategoryDao;
import com.probiz.estore.common.model.catalog.ContentCategory;
import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.content.dao.AutoEvalRelatedContentDao;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;

public class AutoEvalRelatedContentDaoImpl extends HibernateGenericDaoImpl<Content> implements AutoEvalRelatedContentDao {
	private ContentCategoryDao contentCategoryDao;
	
	public void setContentCategoryDao(ContentCategoryDao contentCategoryDao) {
		this.contentCategoryDao = contentCategoryDao;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.dao.AutoEvalRelatedContentDao#getSameKeywordsContentsBySourceId(java.lang.Integer, int, int)
	 */
	public List<Content> getSameKeywordsContentsBySourceId(final Integer sourceId,final  int firstResult,final   int maxResults) {
		final Content content = (Content) getHibernateTemplate().get(Content.class, sourceId);
		final String[] searchValues = (content.getContentTitle()+","+content.getMetaKeyword()).replace("，", ",").split(",");
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(200);
				sb.append("select distinct(c) from Content c where ");
				sb.append("  c.status = 1");
				sb.append(" and c.category.categoryPath like ?");
				sb.append(" and c.contentId <> " + sourceId);
				if(searchValues.length != 0){
					sb.append(" and (");
					for(int i=0; i<searchValues.length; i++){
						sb.append(" c.metaKeyword like ?").append(" or ");
					}
					sb.append(" 1=0 )");
				}
				sb.append(" order by  c.contentId desc");
				Query query = session.createQuery(sb.toString());
				query.setString(0, content.getCategory().getCategoryPath() + "%");
				if(searchValues.length != 0){
					for(int i=0; i<searchValues.length; i++){
						query.setString(i+1, "%"+searchValues[i]+"%");
					}
				}
				query.setFirstResult(firstResult);
				if (maxResults != -1)
					query.setMaxResults(maxResults);
				List<Content> rows = (List<Content>) query.list();

				return rows;
			}

		});
		return list;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.dao.AutoEvalRelatedContentDao#getNewestContentsBySourceId(java.lang.Integer, int, int)
	 */
	public List<Content> getNewestContentsBySourceId(final Integer sourceId, final int firstResult, final int maxResults){
		final ContentCategory category = contentCategoryDao.getById(sourceId);
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(200);
				sb.append("select distinct(c) from Content c where ");
				sb.append("  c.status = 1");
				sb.append(" and c.category.categoryPath like ?");
				sb.append(" order by c.publishTime desc");
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(firstResult);
				query.setString(0, category.getCategoryPath() + "%");
				if (maxResults != -1)
					query.setMaxResults(maxResults);
				List<Content> rows = (List<Content>) query.list();

				return rows;
			}

		});
		return list;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.dao.AutoEvalRelatedContentDao#getViewCountContentsBySourceId(java.lang.Integer, int, int)
	 */
	public List<Content> getViewCountContentsBySourceId(final Integer sourceId,	final int firstResult, final int maxResults){
		final ContentCategory category = contentCategoryDao.getById(sourceId);
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(200);
				sb.append("select distinct(c) from Content c where ");
				sb.append("  c.status = 1");
				sb.append(" and c.category.categoryPath like ?");
				sb.append(" order by c.contentStat.viewCount desc");
				Query query = session.createQuery(sb.toString());
				query.setString(0, category.getCategoryPath() + "%");
				query.setFirstResult(firstResult);
				if (maxResults != -1)
					query.setMaxResults(maxResults);
				List<Content> rows = (List<Content>) query.list();

				return rows;
			}

		});
		return list;
	}
	
	

}
